/**
 * 
 */
package at.ac.tuwien.swa075.telco.auc.dao.hibernate;

import java.text.MessageFormat;
import java.util.ResourceBundle;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import at.ac.tuwien.swa075.telco.auc.AucException;
import at.ac.tuwien.swa075.telco.auc.dao.IProfileDAO;
import at.ac.tuwien.swa075.telco.auc.dao.ProfileTO;

/**
 * A hibernate based implementation of the profile data access object.
 * 
 * @author Johannes Mayr <johannes.mayr@chello.at>
 */
public class HibernateProfileDAO implements IProfileDAO {
	
	private static SessionFactory sessionFactory;
	public HibernateProfileDAO() {
		if (sessionFactory == null) {
			sessionFactory = new Configuration().configure().buildSessionFactory();
		}
	}

	public boolean deleteProfile(ProfileTO profile) throws AucException {
		Session session = null;
		boolean retVal = false;
		try {
			session = sessionFactory.openSession();
			Transaction transaction = session.beginTransaction();
			session.delete(profile);
			retVal = session.isDirty();
			transaction.commit();
		} catch (Exception e) {
			ResourceBundle bundle = ResourceBundle.getBundle("messages.auc-messages");
			throw new AucException(MessageFormat.format(bundle.getString("AUCE007"), this.getClass().getName()), e);
		} finally {
			if (session != null) {
				session.flush();
				session.close();
			}
		}
		return retVal;
	}

	public ProfileTO findProfile(String imsi) throws AucException {
		Session session = null;
		ProfileTO profile = null;
		try {
			session = sessionFactory.openSession();
			Criteria criteria = session.createCriteria(ProfileTO.class);
			criteria.add(Restrictions.eq("imsi", imsi));
			profile = (ProfileTO) criteria.uniqueResult();
		} catch (Exception e) {
			ResourceBundle bundle = ResourceBundle.getBundle("messages.auc-messages");
			throw new AucException(MessageFormat.format(bundle.getString("AUCE008"), this.getClass().getName()), e);
		} finally {
			if (session != null) {
				session.flush();
				session.close();
			}
		}
		return profile;
	}

	public long insertProfile(ProfileTO profile) throws AucException {
		Session session = null;
		try {
			session = sessionFactory.openSession();
			Transaction transaction = session.beginTransaction();
			session.save(profile);
			transaction.commit();
		} catch (Exception e) {
			ResourceBundle bundle = ResourceBundle.getBundle("messages.auc-messages");
			throw new AucException(MessageFormat.format(bundle.getString("AUCE009"), this.getClass().getName()), e);
		} finally {
			if (session != null) {
				session.flush();
				session.close();
			}
		}
		return profile.id;
	}

	public boolean updateProfile(ProfileTO profile) throws AucException {
		Session session = null;
		boolean retVal = false;
		try {
			session = sessionFactory.openSession();
			Transaction transaction = session.beginTransaction();
			
			session.update(profile);
			retVal = session.isDirty();
			transaction.commit();
		} catch (Exception e) {
			ResourceBundle bundle = ResourceBundle.getBundle("messages.auc-messages");
			throw new AucException(MessageFormat.format(bundle.getString("AUCE010"), this.getClass().getName()), e);
		} finally {
			if (session != null) {
				session.flush();
				session.close();
			}
		}
		return retVal;
	}
}
